/* $RCSFile: UploadedFileDataType2.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/UploadedFileDataType2.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;

using Trace2    =   System.Diagnostics.Trace;
using Path2     =   AddonNice.Settings.Path;

namespace AddonNice.UI.DataTypes
{
    public enum UploadFileType { Image, Media, Flash, SilverLight, Document, Template,AllFiles };

	/// <summary>
	/// Allows upload a file on current portal folder
	/// </summary>
	public class UploadedFileDataType2 : BaseDataType
	{
        
        UploadFileType _FileType    =   UploadFileType.Image;
       
        public UploadedFileDataType2() : base()
	    {
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UploadedFileDataType2 Ctor BEG.",DataTypeTraceSwitch.Sw.Info);
	    }

        public UploadedFileDataType2(UploadFileType FType) : base()
	    {
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 Ctor FType: {0}.",FType),DataTypeTraceSwitch.Sw.Info);
            _FileType   =   FType;
	    }


		public override string Description
		{
			get
			{
				return "A file that can be uploaded to server";
			}
		}
		
		protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UploadedFileDataType2 InitializeComponents BEG.",DataTypeTraceSwitch.Sw.Info);
			FileExplorerTextBox upload     =   new FileExplorerTextBox();
			upload.AllowEditTextBox         =   true;
			upload.Width                    =   controlWidth;
			innerControl                    =   upload;
            _IsInitialized                  =   true;
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UploadedFileDataType2 InitializeComponents END.",DataTypeTraceSwitch.Sw.Info);
		}

        public override void DataBind()
        {
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UploadedFileDataType2 DataBind BEG.",DataTypeTraceSwitch.Sw.Info);
			try
			{
				FileExplorerTextBox upload =   (FileExplorerTextBox) innerControl;
				upload.UploadDirectory      =   UploadDirectory;
                upload.FileType             =    _FileType;
				upload.Text                 =   Val;
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 DataBind upload.UploadDirectory: {0}, upload.Text: {1}.",
                        upload.UploadDirectory,upload.Text),DataTypeTraceSwitch.Sw.Info);
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UploadedFileDataType2 DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UploadedFileDataType2 DataBind END.",DataTypeTraceSwitch.Sw.Info);
        }

        /// <summary>
        /// Virtual Path of default image folder on server
        /// </summary>
        protected string UploadDirectory
		{
			get
			{
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( "[V]UploadedFileDataType2 UploadDirectory GET BEG.",DataTypeTraceSwitch.Sw.Info);
				// Obtain PortalSettings from Current Context
                string _Path     =   string.Empty;
				try
				{
					if (portalSettings != null  )
					{
                        switch (_FileType)
                        {
                            default:
                            case UploadFileType.Image:
                                _Path = portalSettings.DefaultImageVirtualPath;
                                break;
                            case UploadFileType.Media:
                                _Path = portalSettings.DefaultMediaVirtualPath;
                                break;
                            case UploadFileType.Flash:
                                _Path = portalSettings.DefaultFlashVirtualPath;
                                break;
                            case UploadFileType.SilverLight:
                                _Path = portalSettings.DefaultSilverlightVirtualPath;
                                break;
                            case UploadFileType.Document:
                                _Path = portalSettings.DefaultDocumentVirtualPath;
                                break;
                            case UploadFileType.Template:
                                _Path = portalSettings.DefaultTemplateVirtualPath;
                                break;
                        }
					}
				}
				catch (Exception exp)
				{
                    if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UploadedFileDataType2 UploadDirectory ex : {0}", exp),DataTypeTraceSwitch.Sw.Info);
                    throw;
				}
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 UploadDirectory GET END _Path: {0}", _Path),DataTypeTraceSwitch.Sw.Info);
				return _Path;
			}	
		}

		private string _innerFullPath;
		/// <summary>
		/// Combine PortalPathPrefix and Val to give a full path
		/// </summary>
		public override string FullPath
		{
			get
			{
				if(_innerFullPath == null )
				{
                    string vl   =   Val;
                    if ( !vl.StartsWith(UploadDirectory)  )
					     _innerFullPath          =  Path2.WebPathCombine(UploadDirectory,vl);
                    else _innerFullPath          =  vl;
				}
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 FullPath GET _innerFullPath: {0}", _innerFullPath),DataTypeTraceSwitch.Sw.Info);
				return _innerFullPath;
			}	
		}

        public override Control EditControl
		{
			get
			{
				if (innerControl == null)
					InitializeComponents();
				FileExplorerTextBox upload =   (FileExplorerTextBox) innerControl;
				upload.UploadDirectory      =   UploadDirectory;
				upload.Text                 =   Val;

                if ( ((FileExplorerTextBox) innerControl).Width != controlWidth )
                    ((FileExplorerTextBox) innerControl).Width = controlWidth;
				return innerControl;
			}
			set
			{
				if( value is FileExplorerTextBox )
				{
					if ( innerControl !=   value ) // normally nether, TBD: need to change the way values are collected
					    innerControl            =   value;
					FileExplorerTextBox upload  =   (FileExplorerTextBox) innerControl;
                    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 EditControl SET BEG UploadDirectory: {0}, Text: {1}.",
                            upload.UploadDirectory,upload.Text),DataTypeTraceSwitch.Sw.Info);
					Val                         =   upload.Text;
                    return;
				}
            string msg  =   "[E]UploadedFileDataType2 EditControl an FileExplorerTextBox value is required, a '" + value.GetType().Name + "' is given.";
            if ( DataTypeTraceSwitch.Sw.TraceError )
                Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
    		throw new ArgumentException(msg, "EditControl");
			}
		}

        public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 Val SET BEG value: {0}", value),DataTypeTraceSwitch.Sw.Info);
                if ( !value.StartsWith(UploadDirectory)  )
                        innerValue      =   value;
                else    innerValue      =   value.Substring(UploadDirectory.Length);
                _innerFullPath  =   null;
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UploadedFileDataType2 Val SET END innerValue: {0}", innerValue),DataTypeTraceSwitch.Sw.Info);
			}
		}
	}
}